using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;

public class VChat : MonoBehaviour
{
    List<string> chats;
    public TextMeshProUGUI tmp;
    public Button button;
    private void Start()
    {
        chats = new(){
            "HELLO",
            "IM DAKA大咖",
            "你需要做什么"
        };
        Observer p = new TriggerObserver();
        Observer head = p;
        //忘记了, 这里犯了个大错 , 因为node'是存在clone的, 所以直接用node是无法拿到克隆后的自己的 , 外部闭包下几乎没办法解决自引用问题
        //这些observer是可以生成的, 也是可以回溯的,
        foreach (var ch in chats)
        {
            string chat = ch;
            FuncObserver node = new FuncObserver()
            {
                name = chat,
            };
            node.start = () =>
            {
                tmp.text = chat;
                nodeToInvoke.Add(node.SetComplete);
            };
            p = p.Next(node);
        }
        head.Subscribe();
        button.onClick.AddListener(() => ChatInvoke());
        
    }

    public List<Action> nodeToInvoke = new List<Action>();
    public void ChatInvoke()
    {
        foreach (var node in nodeToInvoke)
        {
            node.Invoke();
        }
        nodeToInvoke.Clear();
    }
    //根据持久化数据选择加载的任务数据和代码 or 显示消息

    // 所以问题来了 , 交互过程中的交互链条是动态生成的还是静态生成的
    // 交互链其实是一整条timeline , 只不过附加一条可跳过的轴节点, 
    void Per()
    {
        // x.load(task1);
        //所以这个地方产生了自回调和  外部监测回调, 实际上一定有一个交互单元在交互体内or外部挂上去
        //外部挂上去+配点比较自动

        //根据本地存档系数分配该场景需要挂点的条目,Name or Index索引
        // block.foreach (x=> x.pivot.attach(x.task));
        newGuide.FindRuntime();
        newGuide.BindInteractPrefab();



        newGuide.ip.BindAction(button.onClick);

    }
    public GameObject InteractPrefab;
    public TaskPair newGuide;

    public void GuideInit()
    {
        newGuide = new TaskPair()
        {
            taskName = "新手引导",
            taskDesc = "特地为新手准备",
            pivotName = "P_GUIDE",
            interactPrefab = InteractPrefab
        };
    }


    public class TaskPair
    {
        public GameObject runtimePivot;
        public GameObject runtimeIp;
        public GameObject interactPrefab;
        public string taskName;
        public string taskDesc;
        public string pivotName;

        public VChat_InteractBox ip;

        //这里牵扯具体的逻辑, 但是如果交给策划配置,那么就需要伪代码or低代码
        public List<string> dialogs;
        public List<string> reward;

        public void FindRuntime()
        {
            runtimePivot = GameObject.Find(pivotName);
        }

        public void BindInteractPrefab()
        {
            runtimeIp = GameObject.Instantiate(interactPrefab);
            runtimeIp.transform.SetParent(runtimePivot.transform);
            ip = runtimeIp.GetComponent<VChat_InteractBox>();
        }
    }
}
